Databases - MongoDB
About
-
-
It is asked if you want to install Compass during the installation.
-
-
Document-based, very flexible, good for large volumes of JSON data.
-
The local DB data is located in
C:\Program Files\MongoDB\Server\8.0\data, but should be accessed via a connection, for security reasons. -
Is it OOP?
-
Why MongoDB is not an OODBMS (Object-Oriented Database Management Systems):
-
MongoDB does not implement encapsulation, inheritance, or polymorphism, which are fundamental pillars of object-oriented databases (OODBMS). It does not store objects directly with associated methods and behaviors.
-
In MongoDB:
-
Data is stored as BSON documents (a binary version of JSON).
-
Documents represent objects with attributes (keys and values), but not behaviors (methods).
-
There is no native support for concepts like inheritance or direct references between objects.
-
-
-
In summary, it is OOP-like, but not OODBMS.
-
Export and Import
-
Can export in JSON or CSV.
-
Can import in JSON or CSV.
CLI
-
Tutorial using MongoDB Compass {Playlist with 21 videos (4 to 14)} .
-
The first 3 videos are unnecessary.
-
Videos 5 to 14 talk about CLI.
-
Videos 14 to 23 are about a ~primitive connection with Node.js and creation of an HTTP API.
-
Video 19 explains about Postman, to test HTTP calls.
-
Video 23 talks about "pagination", referring to the classic 'queries' of the URL.
-
-
mongosh-
Enters the MongoDB Shell, if you are using an external shell.
-
-
help-
Shows all MongoDB Shell commands, with explanations.
-
-
show dbs-
Returns all dbs.
-
-
use bookstore-
Changes to the bookstore db.
-
-
db-
Returns the current db.
-
-
show collections-
Returns all collections within the current db.
-
-
db.books-
Returns the books collection, within the current db.
-
-
db.books.insertOne({titulo: "Cor da magia", autor: "Terry", rating: 9})-
Adds a new document within the Books collection of the current db.
-
-
db.books.insertMany(_array_de_dict_)-
Adds several new documents within the Books collection of the current db.
-
-
db.books.deleteOne(_filtro_)-
Removes a document, identified by the filter.
-
Ideally,
{_id: ObjectId("_numero_")}is used, to ensure that the correct document is being deleted.
-
-
db.books.deleteMany({autor: "Terry"})-
Deletes multiple documents, identified by the filter.
-
-
db.books.updateOne({_id: ObjectId(_numero_)}, {$set: {rating: 8, paginas: 200}})-
Update Operators are used.
-
Updates the rating and the number of pages of the object identified by the ID.
-
-
db.books.updateMany({autor: "Terry"}, {$inc: {paginas: 5}})-
Update Operators are used.
-
Increases the number of pages of the objects identified by the filter.
-
-
db.books.find(_filtro_opcional_)-
A filter can be passed as an optional parameter, as explained in the session below.
-
Within the Shell:
-
Returns the first 20 documents within the collection.
-
When typing
it, the next 20 documents are displayed.
-
-
-
db.books.findOne(_filtro_)-
Returns the first entry found, obtained by the filter.
-
-
db.books.find().count()-
Returns a number with the amount of entries found by
.find().
-
-
db.books.find().limit(3)-
Returns only 3 entries found by
.find().
-
-
db.books.find().sort({titulo: 1})-
Format:
-
{ entry: 1 or -1 }.
-
1:
-
Ascending.
-
-
-1:
-
Descending.
-
-
-
Returns the entries found by
.find(), sorted in Ascending order.
-
Query / Filters
-
In MongoDB Compass:
-
First argument: Filter
-
{rating: 9}-
Only the documents where the rating is 9.
-
-
{genero: "magic"}-
Returns the documents that contain the genre "magic" within the 'genre' array.
-
-
{genero: ["magic"]}-
Returns the documents that contain ONLY the genre "magic" within the 'genre' array.
-
-
-
Second argument: Project
-
Format:
-
{ entry: 1 or 0 }.
-
-
The '_id' always comes along, unless you use
{id: 0}in the second parameter. -
{autor: "Terry"}, {titulo: 1}-
Only the documents where the 'author' is Terry, displaying only the 'title' entries, with the _id.
-
-
{}, {titulo: 1, _id: 0}-
All documents, displaying only the title of each document, without the _id.
-
-
Query Operators
-
**greater than:
-
db.books.find({rating: {$gt: 7}})-
Returns all books with a rating greater than 7, without including 7.
-
-
-
**or:
-
db.books.find({$or: [{rating: 7}, {rating: 9}]})-
Returns all books that have a rating equal to 7 or 9.
-
-
-
**in:
-
Super similar to or
-
db.books.find({rating: {$in: [7, 8, 9]}})-
Returns all books that have a rating equal to 7, 8 or 9.
-
-
-
**not in:
-
db.books.find({rating: {$nin: [7, 8, 9]}})-
Returns all books that have a rating different from 7, 8 or 9.
-
-
Update Operators
-
**push
-
Adds an entry.
-
-
**pull
-
Removes an entry.
-
Schema without Mongoose
-
In MongoDB, schemas are not required because it is schema-less .
-
However, you can implement document validations through the JSON schema validation feature.
db.createCollection("users", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name", "email", "password"],
properties: {
name: {
bsonType: "string",
description: "The user's name must be a string and is required."
},
email: {
bsonType: "string",
pattern: "^.+@.+\\..+$",
description: "The email must be a valid string and is required."
},
password: {
bsonType: "string",
minLength: 6,
description: "The password must be at least 6 characters long."
},
createdAt: {
bsonType: "date",
description: "Creation date, default is the current date."
}
}
}
}
});
Godot Driver
-
-
Only for godot in Mono C#.
-